package com.region.integral.common_user.obtain.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.common_user.obtain.dao.IIntegralRegister;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.common_user.pojo.CUIEntity;
import com.region.integral.common_user.pojo.KCurrencyEntity;
import com.region.integral.common_user.pojo.LimitEntity;
import com.region.integral.common_user.xml.Regulation;
import com.region.integral.common_user.xml.dto.Register;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.CommonUserLogUtil;
import com.region.integral.util.ExceptionPropertiesUtil;
import com.region.integral.util.IntegralExceptionConstants;
import com.region.integral.util.XMLRegulationUtil;
@Component("iRegister")
public class IntegralRegisterImpl extends BaseHibernate implements
		IIntegralRegister {

	@Override
	public void register(int userid, IntegralData integralData)
			throws IntegralException {
		validate(userid);
		List<CUIEntity> list = hibernateTemplate
				.find("FROM CUIEntity WHERE userid = " + userid);
		List<LimitEntity> limit = hibernateTemplate
		.find("FROM LimitEntity WHERE userid = " + userid);
		if (list.isEmpty()&& limit.isEmpty()) {
			Regulation regulation = XMLRegulationUtil.unmarshaller();
			List<Register> registers = regulation.getRegister();

			for (Register register : registers) {
				int currIntegral = register.getIntegral();
				KCurrencyEntity kCurrencyEntity = new KCurrencyEntity();
				kCurrencyEntity.setUserid(userid);
				kCurrencyEntity.setKcurrency(0);
				CUIEntity cuiEntity = new CUIEntity();
				cuiEntity.setUserid(userid);
				cuiEntity.setCurrIntegral(currIntegral);
				cuiEntity.setkCurrency(kCurrencyEntity);
				cuiEntity.setLastLoginDate(new Date());
				cuiEntity.setMaxIntegral(currIntegral);
				integralData.setSuccee(true);
				hibernateTemplate.save(cuiEntity);
				hibernateTemplate.save(CommonUserLogUtil.log(userid, currIntegral, "register"));
			}
			
			initLimit(userid);
		}
		else{
			throw new IntegralException(ExceptionPropertiesUtil
					.getExceptionMessage(IntegralExceptionConstants.USER_EXISTED));
		}

	}
	private void initLimit(int userid){
		LimitEntity limitEntity = new LimitEntity();
		limitEntity.setUserid(userid);
		limitEntity.setLastAuth(new Date());
		limitEntity.setLastCommentGoods(new Date());
		limitEntity.setLastCommentWeiBo(new Date());
		limitEntity.setLastGroupPeopleRise(new Date());
		limitEntity.setLastInviteFriend(new Date());
		limitEntity.setLastissueActivity(new Date());
		limitEntity.setLastJoinGroup(new Date());
		limitEntity.setLastJoinActivity(new Date());
		limitEntity.setLastReplyComment(new Date());
		limitEntity.setLastSendWeiBo(new Date());
		hibernateTemplate.save(limitEntity);
	}

}
